"""
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.


给两个非空的链表，每个节点包含一个整数。数字是以倒序排列的，现在输出两个链表相加得出的新链表。
"""
# 链表对象为ListNode
class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


# 先建立一个虚拟头节点，这个头节点不存储东西，单纯为了最后返回头节点
def addTwoNumbers(L1,L2):   # L1，L2为两个链表
    res = ListNode(0)
    re = res
    carry = 0   # 进位信息

    while(L1 or L2):
        num1 = L1.val if L1 else 0
        num2 = L2.val if L2 else 0
        re.next = ListNode((carry + num2 + num1) % 10)
        carry = (carry + num1 + num2) // 10
        re = re.next
        if(L1 != None):L1 = L1.next
        if(L2 != None):L2 = L2.next
    if carry > 0:
        re.next = ListNode(carry)
    return res.next

# 优化内存消耗,这样的话没有新建链表，接着使用了原来的链表。
# 提交所用
class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        res = ListNode(0)
        re = res
        carry = 0   # 进位信息

        while(l1 and l2):
            num1 = l1.val
            num2 = l2.val
            l1.val = (carry + num2 + num1) % 10
            carry = (carry + num1 + num2) // 10
            re.next = l1
            re = re.next
            l1 = l1.next
            l2 = l2.next
        while l1:   # l1列表比较长
            num1 = l1.val
            l1.val = (carry + num1) % 10
            carry = (carry + num1) // 10
            re.next = l1
            re = re.next
            l1 = l1.next
        while l2:   # l1列表比较长
            num2 = l2.val
            l2.val = (carry + num2) % 10
            carry = (carry + num2) // 10
            re.next = l2
            re = re.next
            l2 = l2.next
        if carry > 0:
            re.next = ListNode(carry)
        return res.next


# 提交所用
class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        res = ListNode(0)
        re = res
        carry = 0   # 进位信息

        while(l1 or l2):
            num1 = l1.val if l1 else 0
            num2 = l2.val if l2 else 0
            re.next = ListNode((carry + num2 + num1) % 10)
            carry = (carry + num1 + num2) // 10
            re = re.next
            if(l1 != None):l1 = l1.next
            if(l2 != None):l2 = l2.next
        if carry > 0:
            re.next = ListNode(carry)
        return res.next

# 这种方法和第一种不优化内存的相同。就是代码显得厉害。
class Solution:
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        def dfs(l, r, i):
            if not l and not r and not i: return None
            s = (l.val if l else 0) + (r.val if r else 0) + i
            node = ListNode(s % 10)
            node.next = dfs(l.next if l else None, r.next if r else None, s // 10)
            return node
        return dfs(l1, l2, 0)